define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index' + location.search,
                    add_url: 'goods/goods/add',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='SelfCateId']", form).addClass("selectpage").data("source", "category/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'goodsid', title: '商品ID'},
                        {field: 'SelfCateId', title: '分类Id', operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'title', title: __('Title'),operate: 'LIKE %...%'},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'sales', title: __('Sales'),operate:false},
                        {field: 'discount', title: '拼团折扣(%)'},
                        {field: 'weight', title:'首页排序', operate:'BETWEEN'},
                        {
                            field: 'atIndex',
                            title: '首页',
                            align: 'center',
                            formatter: Table.api.formatter.toggle
                        },

                        // {field: 'category', title: __('Category')},
                        // {field: 'SelfCateId', title: __('Selfcateid')},
                        // {field: 'indexCategory',title: '首页品类'},
                        // {field: 'recommendId',title: '首页推荐位'},
                        // {field: 'mtime', title: __('Mtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'status',
                            title: '上架',
                            align: 'center',operate:false,
                            formatter: Table.api.formatter.toggle
                        },
                        {field: 'ctime', title: __('Ctime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});